//
//  ViewController.m
//  deviceFUN
//
//  Created by nnandzc on 15/10/22.
//  Copyright © 2015年 nnandzc. All rights reserved.
//

#import "ViewController.h"
#import <MessageUI/MessageUI.h>

@interface ViewController () <MFMessageComposeViewControllerDelegate,MFMailComposeViewControllerDelegate,UINavigationControllerDelegate, UIImagePickerControllerDelegate>
{
    UIWebView *_webView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)messageClicked:(id)sender {
    [self msg2];
}

- (void)msg2
{
    // 判断用户设备能否发送短信
    if (![MFMessageComposeViewController canSendText]) {
        return;
    }
    
    // 1. 实例化一个控制器
    MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
    
    // 2. 设置短信内容
    // 1) 收件人
    controller.recipients = @[@"10010", @"10086"];
    
    // 2) 短信内容
    controller.body = @"给您拜个晚年，祝您晚年快乐！";
    
    // 3) 设置代理
    controller.messageComposeDelegate = self;
    
    // 3. 显示短信控制器
    [self presentViewController:controller animated:YES completion:nil];
}

#pragma mark 短信控制器代理方法
/**
 短信发送结果
 
 MessageComposeResultCancelled,     取消
 MessageComposeResultSent,          发送
 MessageComposeResultFailed         失败
 */
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
{
    NSLog(@"%d", result);
    
    // 在面向对象程序开发中，有一个原则，谁申请，谁释放！
    // *** 此方法也可以正常工作，因为系统会将关闭消息发送给self
    //    [controller dismissViewControllerAnimated:YES completion:nil];
    
    // 应该用这个！！！
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)mailClicked:(id)sender {
    [self sendmail];
}

- (void)sendmail
{
    // 1. 先判断能否发送邮件
    if (![MFMailComposeViewController canSendMail]) {
        // 提示用户设置邮箱
        return;
    }
    
    // 2. 实例化邮件控制器，准备发送邮件
    MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
    
    // 1) 主题 xxx的工作报告
    [controller setSubject:@"我的工作报告"];
    // 2) 收件人
    [controller setToRecipients:@[@"nnadnzc@126.com"]];
    
    // 3) cc 抄送
    // 4) bcc 密送(偷偷地告诉，打个小报告)
    // 5) 正文
    [controller setMessageBody:@"这是我的<font color=\"blue\">工作报告</font>，请审阅！<BR />P.S. 我的头像牛X吗？" isHTML:YES];
    
    // 6) 附件
    UIImage *image = [UIImage imageNamed:@"头像1.png"];
    NSData *imageData = UIImagePNGRepresentation(image);
    // 1> 附件的二进制数据
    // 2> MIMEType 使用什么应用程序打开附件
    // 3> 收件人接收时看到的文件名称
    // 可以添加多个附件
    [controller addAttachmentData:imageData mimeType:@"image/png" fileName:@"头像.png"];
    
    // 7) 设置代理
    [controller setMailComposeDelegate:self];
    
    // 显示控制器
    [self presentViewController:controller animated:YES completion:nil];
}

#pragma mark - 邮件代理方法
/**
 MFMailComposeResultCancelled,      取消
 MFMailComposeResultSaved,          保存邮件
 MFMailComposeResultSent,           已经发送
 MFMailComposeResultFailed          发送失败
 */
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    // 根据不同状态提示用户
    NSLog(@"%d", result);
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)phoneClicked:(id)sender {
    [self tel3];
}

- (void)tel3
{
    // 提示：不要将webView添加到self.view，如果添加会遮挡原有的视图
    // 懒加载
    if (_webView == nil) {
        _webView = [[UIWebView alloc] init];
    }
    NSLog(@"%p", _webView);
    
    //    _webView = [[UIWebView alloc] initWithFrame:self.view.bounds];
    //    [self.view addSubview:_webView];
    
    NSURL *url = [NSURL URLWithString:@"tel://13681424187"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [_webView loadRequest:request];
}


- (IBAction)selectForAlbumButtonClick:(UIButton *)sender
{
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.delegate = self;
    [self presentViewController:imagePickerController animated:YES completion:nil];
    
    imagePickerController.allowsEditing = YES;
    imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}

//拍照获取
- (IBAction)takePickturesButtonClick:(UIButton *)sender
{
     
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
        imagePickerController.delegate = self;
        imagePickerController.allowsEditing = YES;
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        [self presentViewController:imagePickerController animated:YES completion:nil];
       
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(nullable NSDictionary<NSString *,id> *)editingInfo
{
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
    imageView.image = image;
    [self.view addSubview:imageView];
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
